﻿package mortal.game.view.chat.selectPanel
{
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.vip.panel.*;

    public class FacePanel extends Sprite
    {
        private var _dicFaceMcNumber:Dictionary;
        private var _tfNotHaveFace:VipOpenTipsPanel;
        private var _tabBarData:Array;
        private var _tabBar:GTabBar;
        private var _normalPanel:Sprite;
        private var _vipPanel:Sprite;
        private var _maskSp:Sprite;
        private static var _instance:FacePanel;
        private static var _currentBtn:InteractiveObject;
        private static var _dicbtnCallback:Dictionary = new Dictionary();

        public function FacePanel()
        {
            this._dicFaceMcNumber = new Dictionary();
            this.createChildren();
            return;
        }// end function

        private function createChildren() : void
        {
            var _loc_1:* = ResourceConst.getScaleBitmap("ToolTipBg");
            _loc_1.width = 310;
            _loc_1.height = 185;
            this.addChild(_loc_1);
            UIFactory.bg(10, 155, 299, 1, this, "SplitLine");
            this._tabBarData = [{name:Language.getString(11586), label:Language.getString(11586)}, {name:Language.getString(11587), label:Language.getString(11587)}];
            this._tabBar = UIFactory.gTabBar(8, 157, this._tabBarData, 62, 22, this, this.onTabBarChange);
            this._tabBar.buttonStyleName = "ChatBtn";
            var _loc_2:* = GlobalStyle.textFormatBai;
            _loc_2.center();
            this._tfNotHaveFace = UICompomentPool.getUICompoment(VipOpenTipsPanel);
            this._tfNotHaveFace.move(30, 70);
            this._tfNotHaveFace.resetLevelTextPos(62, 9);
            this._tfNotHaveFace.bgUrl = ImagesConst.vipOpenTipsBg_mid;
            this._tfNotHaveFace.valueType = VipOpenTipsPanel.noValue;
            this._tfNotHaveFace.vipType = EVipRecordType._EVipRecordTypeExpress;
            this.addChild(this._tfNotHaveFace);
            this._normalPanel = new Sprite();
            this.addChild(this._normalPanel);
            this._vipPanel = new Sprite();
            this._vipPanel.visible = false;
            this.addChild(this._vipPanel);
            this.createNormal();
            this.createVIP();
            this.layOut();
            this.addChild(this._tfNotHaveFace);
            this.addEventListener(Event.ADDED_TO_STAGE, this.layOut);
            return;
        }// end function

        private function createNormal() : void
        {
            this.addFace();
            return;
        }// end function

        private function addFace() : void
        {
            var _loc_2:Sprite = null;
            var _loc_3:SWFPlayer = null;
            var _loc_1:int = 1;
            while (_loc_1 <= 72)
            {
                
                _loc_2 = new Sprite();
                _loc_3 = UIFactory.getUICompoment(SWFPlayer);
                _loc_3.timeRate = 2;
                _loc_3.move(0, 0);
                _loc_3.load("face" + _loc_1 + ".swf", ModelType.NormalSwf, null);
                _loc_2.addChild(_loc_3);
                _loc_2.x = 5 + 25 * ((_loc_1 - 1) % 12);
                _loc_2.y = 5 + 25 * Math.floor((_loc_1 - 1) / 12);
                _loc_2.mouseEnabled = true;
                _loc_2.buttonMode = true;
                this._normalPanel.addChild(_loc_2);
                _loc_2.addEventListener(MouseEvent.MOUSE_OVER, this.selectHandler);
                _loc_2.addEventListener(MouseEvent.MOUSE_OUT, this.selectHandler);
                _loc_2.addEventListener(MouseEvent.CLICK, this.choseFaceHandler);
                this._dicFaceMcNumber[_loc_2] = _loc_1;
                _loc_1++;
            }
            return;
        }// end function

        private function createVIP() : void
        {
            this.addFaceVIP();
            this.layOut();
            return;
        }// end function

        private function addFaceVIP() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Bitmap = null;
            var _loc_3:Sprite = null;
            var _loc_4:SWFPlayer = null;
            _loc_1 = 1;
            while (_loc_1 <= 14)
            {
                
                _loc_3 = new Sprite();
                _loc_4 = UIFactory.getUICompoment(SWFPlayer);
                _loc_4.timeRate = 2;
                _loc_4.move(25, 25);
                _loc_4.load("bface" + _loc_1 + ".swf", ModelType.NormalSwf, null);
                _loc_3.addChild(_loc_4);
                _loc_3.x = 11 + 60 * ((_loc_1 - 1) % 5);
                _loc_3.y = 8 + 50 * Math.floor((_loc_1 - 1) / 5);
                _loc_3.mouseEnabled = true;
                _loc_3.buttonMode = true;
                this._vipPanel.addChild(_loc_3);
                _loc_3.addEventListener(MouseEvent.MOUSE_OVER, this.selectHandler);
                _loc_3.addEventListener(MouseEvent.MOUSE_OUT, this.selectHandler);
                _loc_3.addEventListener(MouseEvent.CLICK, this.choseFaceHandler);
                this._dicFaceMcNumber[_loc_3] = "b" + _loc_1;
                _loc_1++;
            }
            this._maskSp = new Sprite();
            _loc_2 = new Bitmap();
            _loc_2.x = 3;
            _loc_2.y = 3;
            _loc_2.bitmapData = new BitmapData(304, 154, true, 1728053247);
            _loc_2.visible = false;
            this._maskSp.addChild(_loc_2);
            this.addChild(this._maskSp);
            return;
        }// end function

        public function layOut(param1 = null) : void
        {
            this._tfNotHaveFace.visible = false;
            this._normalPanel.visible = false;
            this._vipPanel.visible = false;
            this._maskSp.visible = false;
            this._tfNotHaveFace.visible = false;
            var _loc_2:* = this._tabBar.selectedIndex;
            if (_loc_2 == 0)
            {
                this._normalPanel.visible = true;
            }
            else if (_loc_2 == 1)
            {
                if (!VipConfig.instance.isShowEffect(EVipRecordType._EVipRecordTypeExpress, Cache.instance.role.entityInfo.VIP))
                {
                    this._maskSp.visible = true;
                    this._tfNotHaveFace.visible = true;
                }
                else
                {
                    this._vipPanel.visible = true;
                }
            }
            return;
        }// end function

        private function onTabBarChange(event:MuiEvent) : void
        {
            this.layOut();
            return;
        }// end function

        private function selectHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as Sprite;
            if (event.type == MouseEvent.MOUSE_OVER)
            {
                _loc_2.filters = [FilterConst.itemChooseFilter];
            }
            if (event.type == MouseEvent.MOUSE_OUT)
            {
                _loc_2.filters = [];
            }
            return;
        }// end function

        private function choseFaceHandler(event:MouseEvent) : void
        {
            FacePanel.hide();
            (_dicbtnCallback[_currentBtn] as Function).call(null, this._dicFaceMcNumber[event.currentTarget as Sprite]);
            return;
        }// end function

        override public function set visible(param1:Boolean) : void
        {
            super.visible = param1;
            if (param1 && Global.stage.contains(this))
            {
                this.startAllFace();
            }
            else
            {
                this.stopAllFace();
            }
            return;
        }// end function

        public function stopAllFace() : void
        {
            var _loc_1:* = undefined;
            var _loc_2:DisplayObject = null;
            for (_loc_1 in this._dicFaceMcNumber)
            {
                
                if (_loc_1 is Sprite)
                {
                    _loc_2 = (_loc_1 as Sprite).getChildAt(0);
                    if (_loc_2 is SWFPlayer)
                    {
                        (_loc_2 as SWFPlayer).stop();
                    }
                }
            }
            return;
        }// end function

        public function startAllFace() : void
        {
            var _loc_1:* = undefined;
            var _loc_2:DisplayObject = null;
            for (_loc_1 in this._dicFaceMcNumber)
            {
                
                if (_loc_1 is Sprite)
                {
                    _loc_2 = (_loc_1 as Sprite).getChildAt(0);
                    if (_loc_2 is SWFPlayer)
                    {
                        (_loc_2 as SWFPlayer).play();
                    }
                }
            }
            return;
        }// end function

        public static function get instance() : FacePanel
        {
            if (!_instance)
            {
                _instance = new FacePanel;
            }
            return _instance;
        }// end function

        public static function registBtn(param1:IFrUI, param2:Function) : void
        {
            _dicbtnCallback[param1] = param2;
            param1.configEventListener(MouseEvent.CLICK, onClickBtn);
            return;
        }// end function

        public static function unRegistBtn(param1:IFrUI) : void
        {
            if (param1 && param1 is InteractiveObject)
            {
                (param1 as InteractiveObject).removeEventListener(MouseEvent.CLICK, onClickBtn);
                delete _dicbtnCallback[param1];
            }
            return;
        }// end function

        private static function onClickBtn(event:MouseEvent) : void
        {
            var _loc_3:Point = null;
            var _loc_2:* = event.target as InteractiveObject;
            if (_loc_2 != _currentBtn && FacePanel.instance.parent)
            {
                _currentBtn = _loc_2;
                _loc_3 = _loc_2.parent.localToGlobal(new Point(_loc_2.x, _loc_2.y));
                FacePanel.instance.x = _loc_3.x;
                FacePanel.instance.y = _loc_3.y - 190;
                event.stopPropagation();
                return;
            }
            _currentBtn = _loc_2;
            if (FacePanel.instance.parent)
            {
                hide();
            }
            else
            {
                _loc_3 = _loc_2.parent.localToGlobal(new Point(_loc_2.x, _loc_2.y));
                FacePanel.instance.x = _loc_3.x;
                FacePanel.instance.y = _loc_3.y - 190;
                LayerManager.toolTipLayer.addChild(FacePanel.instance);
                FacePanel.instance.startAllFace();
                Global.instance.callLater(addFrameClickStage);
            }
            return;
        }// end function

        private static function hide() : void
        {
            FacePanel.instance.parent.removeChild(FacePanel.instance);
            FacePanel.instance.stopAllFace();
            Global.stage.removeEventListener(MouseEvent.CLICK, onClickStage);
            return;
        }// end function

        private static function addFrameClickStage() : void
        {
            Global.stage.addEventListener(MouseEvent.CLICK, onClickStage);
            return;
        }// end function

        private static function onClickStage(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as DisplayObject;
            if (_loc_2 != _currentBtn && _loc_2 != FacePanel.instance && !FacePanel.instance.contains(_loc_2))
            {
                if (FacePanel.instance.parent)
                {
                    hide();
                }
            }
            return;
        }// end function

    }
}
